package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ApiErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.NucTransferService;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.platform.system.nuc.NucApi;
import com.cmes.platform.system.nuc.dto.AdminUserDto;
import com.cmes.platform.system.nuc.dto.UserProfileDTO;
import com.cmes.platform.system.nuc.dto.UserProfileRespDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 港口基础数据转换
 * 
 * @author dev89
 * @date 2025/07/18 09:07
 **/
@Service
@Validated
@Slf4j
public class NucTransferServiceImpl implements NucTransferService {

    @Resource
    private NucApi nucApi;

    private static final String GET_NUC_EXCEPTION = "获取当前登陆用户异常";

    private static final String GET_NUC_NULL = "获取当前用户返回null";

    private static final String GET_NUC_EXCEPTION_BY_NAME = "通过用户名获取用户异常";

    private static final String GET_NUC_NULL_BY_NAME = "通过用户名获取用户返回null";

    @Override
    public UserProfileDTO getUserProfile() {
        CommonResult<UserProfileDTO> commonResult;
        try {
            commonResult = nucApi.getUserProfile();
        } catch (Exception e) {
            log.error(GET_NUC_EXCEPTION, e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.NUC_GET_USER_PROFILE_FAILED);
        }

        if (commonResult == null) {
            log.error(GET_NUC_NULL);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.NUC_GET_USER_PROFILE_FAILED);
        }
        return commonResult.getData();
    }

    @Override
    public UserProfileRespDto getUserProfileByName(String name) {
        CommonResult<UserProfileRespDto> commonResult;
        try {
            commonResult = nucApi.getUserProfileByName(name);
        } catch (Exception e) {
            log.error(GET_NUC_EXCEPTION_BY_NAME, e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.NUC_GET_USER_PROFILE_BY_NAME_FAILED);
        }

        if (commonResult == null || commonResult.getData() == null) {
            log.error(GET_NUC_NULL_BY_NAME);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.NUC_GET_USER_PROFILE_BY_NAME_NULL);
        }
        return commonResult.getData();
    }

    @Override
    public List<AdminUserDto> getUserProfileByIdList(List<Long> userIds) {
        CommonResult<List<AdminUserDto>> commonResult;
        try {
            commonResult = nucApi.getUserProfileByIdList(userIds);
        } catch (Exception e) {
            log.error(GET_NUC_EXCEPTION, e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.NUC_GET_USER_PROFILE_FAILED);
        }
        return commonResult.getCheckedData();
    }

    @Override
    public Map<String, String> getUserProfileByIdMap(List<Long> userIds) {
        Map<String, String> userIdToCodeMap = new HashMap<>(16);
        try {
            CommonResult<List<AdminUserDto>> commonResult = nucApi.getUserProfileByIdList(userIds);
            if (CollectionUtil.isNotEmpty(commonResult.getCheckedData())) {
                return commonResult.getCheckedData().stream().collect(Collectors.toMap(user -> String.valueOf(user.getId()), AdminUserDto::getNickname));
            }
        } catch (Exception e) {
            log.error(GET_NUC_EXCEPTION, e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.NUC_GET_USER_PROFILE_FAILED);
        }
        return userIdToCodeMap;
    }
}
